// JavaScript Document


var page = require('webpage').create(),
  system = require('system'),
  t, address;
var fs = require("fs");

if (system.args.length === 1) {
  console.log('Usage: loadspeed.js <some URL>');
  phantom.exit();
}

page.settings.loadImages = false;  
page.settings.resourceTimeout = 10000;
page.viewportSize = {
  width: 1280,
  height: 800
};

block_urls = ['baidu.com'];
page.onResourceRequested = function(requestData, request){
    for(url in block_urls) {
        if(requestData.url.indexOf(block_urls[url]) !== -1) {
            request.abort();
            //console.log(requestData.url + " aborted");
            return;
        }
    }            
}
t = Date.now();
address = system.args[1];
page.open(address, function(status) {
  if (status !== 'success') {
    console.log('FAIL to load the address');
  } else {
    t = Date.now() - t;

    //  var ua = page.evaluate(function() {
    //   return document.getElementById('companyServiceMod').innerHTML;
      
    // });
    // fs.write("qq.html", ua, 'w');
   // console.log("测试qq: "+ua);  
    console.log('Loading time ' + t + ' msec');
    console.log(page.content);
    setTimeout(function(){ phantom.exit(); }, 6000);
  }
  phantom.exit();
});